package jd.se.admin;

import jd.se.admin.BookControl.Book;
import jd.se.admin.BookControl.BookDto;
import jd.se.admin.BookControl.BookService;
import jd.se.admin.UserControl.UserDto;
import jd.se.admin.UserControl.UserService;
import jd.se.common.Result;
import jd.se.common.Utils;
import jd.se.admin.UserControl.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet({
        "/admin/acceptbook",
        "/admin/addbook",
        "/admin/deletebook",
        "/admin/deleteuser",
        "/admin/searchbook"
})

public class AdminAPI extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String path = req.getServletPath();
        if("/admin/acceptbook".equals(path)){
        BookService bService = new BookService();
        List<Book> bList =  bService.getBookList();
        //通过视图生成json
        //Utils.outJson(response, teaList);
        Utils.outResult(resp, Result.success(bList));
        }
        else if("/admin/deleteuser".equals(path)){
            UserService uService = new UserService();
            List<User> uList =  uService.getUserList();
        //通过视图生成json
        //Utils.outJson(response, teaList);
            Utils.outResult(resp, Result.success(uList));
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String path = req.getServletPath();
        if("/admin/deleteuser".equals(path)){
            try {
                UserService uService = new UserService();
                UserDto dto = Utils.getBeanFromRequest(UserDto.class, req);
                uService.deleteUser(dto);
                Utils.outJson(resp, Result.success());
            }catch(Exception e){
                e.printStackTrace();
                Utils.outJson(resp, Result.fail(Result.ERR_CODE_BUSINESS, e.getMessage()));
            }

        }
        else if("/admin/acceptbook".equals(path)){
            try {
                BookService bService = new BookService();
                BookDto dto = Utils.getBeanFromRequest(BookDto.class, req);
                bService.acceptBook(dto);
                Utils.outJson(resp, Result.success());
            }catch(Exception e){
                e.printStackTrace();
                Utils.outJson(resp, Result.fail(Result.ERR_CODE_BUSINESS, e.getMessage()));
            }
        }
        else if("/admin/addbook".equals(path)){
            try {
                BookService bService = new BookService();
                BookDto dto = Utils.getBeanFromRequest(BookDto.class, req);
                bService.addBook(dto);
                Utils.outJson(resp, Result.success());
            }catch(Exception e){
                e.printStackTrace();
                Utils.outJson(resp, Result.fail(Result.ERR_CODE_BUSINESS, e.getMessage()));
            }
        }
        else if("/admin/deletebook".equals(path)){
            try {
                BookService bService = new BookService();
                BookDto dto = Utils.getBeanFromRequest(BookDto.class, req);
                bService.deleteBook(dto);
                Utils.outJson(resp, Result.success());
            }catch(Exception e){
                e.printStackTrace();
                Utils.outJson(resp, Result.fail(Result.ERR_CODE_BUSINESS, e.getMessage()));
            }
        }
        else if("/admin/searchbook".equals(path)){
            try {
                BookService bService = new BookService();
                BookDto dto = Utils.getBeanFromRequest(BookDto.class, req);
                List<Book> bList = bService.searchBook(dto);
                Utils.outJson(resp, Result.success(bList));
            }catch(Exception e){
                e.printStackTrace();
                Utils.outJson(resp, Result.fail(Result.ERR_CODE_BUSINESS, e.getMessage()));
            }
        }






    }
}
